# type创建的类
def create_class(name):
    if name == 'user':
        class User:
            def __str__(self):
                return 'user'

        return User
    elif name == 'company':
        class Company:
            def __str__(self):
                return 'company'

        return Company


# 通过type动态创建类
# User=type('User',(),{})

def say(self):
    return 'i am user'
    # return self.name


class BaseClass:
    def answer(self):
        return 'i am baseclass'


# 什么事元类，元类是创建类的类，type就是元类
class MetaClass(type):
    def __new__(cls, *args, **kwargs):
        return super().__new__(cls, *args, **kwargs)  # 三个参数必须都传入


class User2(metaclass=MetaClass):
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return self.name


# Python中类实例化过程，会首先寸照metaclass，通过metaclass去创建user类
# type去创建类对象，实例

if __name__ == '__main__':
    MyClass = create_class('user')
    my_obj = MyClass()
    print(type(my_obj))
    User = type('User', (BaseClass,), {'name': 'user', 'say': say})
    my_obj = User()
    print(my_obj.name)
    print(my_obj.say())
    print(my_obj.answer())

    my_obj = User2(name='AA')
    print(my_obj)
